package com.zqw.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zqw.info.GoodsInfo;
import com.zqw.info.GoodsTypeInfo;
import com.zqw.mapper.GoodsMapper;
import com.zqw.mapper.GoodsTypeMapper;
import com.zqw.pojo.GoodsPojo;
import com.zqw.pojo.GoodsTypePojo;
import com.zqw.service.GoodsService;
import com.zqw.ss.Page;
import com.zqw.ss.Transition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    GoodsMapper goodsMapper;

    @Autowired
    GoodsTypeMapper goodsTypeMapper;

    /**
     * 查询商品列表
     * @param
     * @return
     */
    public List<GoodsInfo> queryGoods(GoodsInfo goodsInfo){
        ArrayList<GoodsInfo> goodsInfos = new ArrayList<>();
        GoodsPojo transition = null;
        if(goodsInfo!=null){
            transition = Transition.transition(goodsInfo, GoodsPojo.class);
        }
        List<GoodsPojo> goodsPojos = goodsMapper.queryGoods(transition);
        for (int i = 0; i < goodsPojos.size(); i++) {
            goodsInfos.add(Transition.transition(goodsPojos.get(i),GoodsInfo.class));
        }
        return goodsInfos;
    }

    public Page queryGoodsPage(GoodsInfo goodsInfo){
        ArrayList<GoodsInfo> goodsInfos = new ArrayList<>();
        PageHelper.startPage(goodsInfo.getPageNum(),goodsInfo.getPageSize());
        GoodsPojo transition = null;
        if(goodsInfo!=null){
            transition = Transition.transition(goodsInfo, GoodsPojo.class);
        }
        List<GoodsPojo> goodsPojos = goodsMapper.queryGoods(transition);
        PageInfo<GoodsPojo> pageInfo = new PageInfo<>(goodsPojos);
        goodsPojos = pageInfo.getList();
        for (int i = 0; i < goodsPojos.size(); i++) {
            goodsInfos.add(Transition.transition(goodsPojos.get(i),GoodsInfo.class));
        }
        Page page = new Page();
        page.setList(goodsInfos);
        page.setPageNum(pageInfo.getPageNum());
        page.setPageSize(pageInfo.getPageSize());
        page.setPages(pageInfo.getPages());
        return page;
    }

    /**
     * 查询商品类型列表
     * @param
     * @return
     */
    public List<GoodsTypeInfo> queryGoodsType(){
        ArrayList<GoodsTypeInfo> goodsTypeInfos = new ArrayList<>();
        List<GoodsTypePojo> goodsTypePojos = goodsTypeMapper.queryGoodsType();
        for (int i = 0; i < goodsTypePojos.size(); i++) {
            goodsTypeInfos.add(Transition.transition(goodsTypePojos.get(i),GoodsTypeInfo.class));
        }
        return goodsTypeInfos;
    }

    /**
     * 根据id查询商品详情
     * @param
     * @return
     */
    @Override
    public GoodsInfo queryGoodsById(String gid) {
        GoodsInfo goodsInfo = null;
        GoodsPojo goodsPojo = goodsMapper.queryGoodsById(gid);
        if(null != goodsPojo){
            goodsInfo = Transition.transition(goodsPojo, GoodsInfo.class);
            if(null != goodsPojo.getType()){
                GoodsTypeInfo transition = Transition.transition(goodsPojo.getType(), GoodsTypeInfo.class);
                goodsInfo.setType(transition);
            }
        }
        return goodsInfo;
    }

}
